<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>规则制度</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
</head>
<script>
    $.ajaxSetup({
        async: false
    });
    $(function(){
        $(".layui-bg-red").click(function(){
            $("#tsone").html("");
            $("#tstwo").html("");
        });
        //图片1
        $("#btnone").click(function () {
            $("#fileOne").click();
            $("#fileOne").change(function(obj){
                var fileInput = $('#fileOne').get(0).files[0];//获取input控件的file
                if(fileInput){
                    var str=fileInput.name;
                    //取后缀名
                    $("#tsone").html(str.substring(0,4)+"..."+str.match(/\.([^\.]+)$/)[1]);
                }
            });
        });
    })
</script>
<body>
<form class="layui-form layui-form-pane" id="formDId" action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="pkRegimeId" id ="pkRegimeId" />
    <input type="hidden" id="fileName">
    <div class="layui-form-item">
        <label class="layui-form-label">文件标题</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="regimeName" id ="regimeName" lay-verify="title" autocomplete="off" placeholder="文件标题" lay-verify="required"
                    >
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">文件编号</label>
        <div class="layui-input-block">
            <input type="text" name="regimeCode" id = "regimeCode" autocomplete="off" disabled placeholder="自动生成"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 120px;">规章制度类型</label>
        <div class="layui-input-block"style="margin-left: 120px">
            <div id="regimeTypeId" class="xm-select-demo"></div>
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">所属部门</label>
        <div class="layui-input-block" >
            <select id="departmentId" name="departmentId" lay-verify="required">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">生效时间</label>
        <div class="layui-input-block">
        <input type="text" class="layui-input" id="effectTime" name="effectTime" placeholder="yyyy-MM-dd" lay-verify="required">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">过期时间</label>
        <div class="layui-input-block">
        <input type="text" class="layui-input" id="expiresTime" name="expiresTime"  placeholder="yyyy-MM-dd" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item ">
        <label class="layui-form-label">摘要</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" name="regimeAbstract" id="regimeAbstract"lay-verify="required"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="file" name="fileOne" style="display: none" id="fileOne"  autocomplete="off"
                   class="layui-input">
            <button id="btnone"  type="button" class="layui-btn layui-btn-normal">上传文件</button>
            <a id="tsone" target="_blank" style="text-decoration:none"></a>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block" id="flows">
            <input type="button" id="demo3" class="layui-btn layui-btn-primary" value="取消">
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'laydate','upload'], function () {
        var form = layui.form
            , laydate = layui.laydate;
        var $ = layui.jquery
            , upload = layui.upload;
        //只出现确定按钮
        laydate.render({
            elem: '#expiresTime'
            , btns: ['confirm']
        });

        //只出现确定按钮
        laydate.render({
            elem: '#effectTime'
            , btns: ['confirm']
        });

        //选完文件后不自动上传
        upload.render({
            elem: '#test8'
            , url: 'https://httpbin.org/post' //改成您自己的上传接口
            , auto: false
            //,multiple: true
            , bindAction: '#test8'
            , done: function (res) {
                layer.msg('上传成功');
                console.log(res)
            }
        });

        //需要操作的数据
        let pkRegimeId = getQueryString("pkRegimeId");
        let regimeTypeId;
        let show = getQueryString("show");
        layui.use(['form', 'laydate', 'upload'], function () {
            var form = layui.form
                , laydate = layui.laydate;
            var $ = layui.jquery
                , upload = layui.upload;
            if (pkRegimeId) {
                let regime = getAjaxById(getRootPath() + "OaRegimeAction/findById/" + pkRegimeId);
                regimeTypeId = regime.data.regimeTypeId;
                console.info(regimeTypeId+"类型id");


            }

            //加载组件
            layui.config({
                //引入扩展文件库
                base: getRootPath() + 'res/layui/dist/'
            }).extend({
                //引入对应的文件名
                xmSelect: 'xm-select'
                //拿到对应的文件模块
            }).use(['xmSelect'], function () {
                //对于模块赋值使用
                var xmSelect = layui.xmSelect;
                //渲染多选
                pkJurisdictionId = xmSelect.render({
                    el: '#regimeTypeId',
                    radio: true,
                    layVerify: 'required',
                    name:'regimeTypeId',
                    tree: {
                        show: true,
                        strict: false,
                        expandedKeys: [-1],
                    },
                    height: 'auto',
                    data: loadOaRegimeType(regimeTypeId, show)
                })

            });

            function loadOaRegimeType(id, show) {
                console.info(id)
                let url = getRootPath() + "OaRegimeTypeAction/loadOaRegimeType";
                let data = {
                    id: id, //需要操作的数据
                    show: '2'
                };
                let res = getAjax(url, data);
                if (res) {
                    return res;
                } else {
                    return [];
                }

            }


            //监听起草提交
            form.on('submit(demo1)', function (data) {

                console.info(data.field);
                // 单击之后提交按钮不可选,防止重复提交
                var DISABLED = 'layui-btn-disabled';
                // 增加样式
                $('.site-demo-active').addClass(DISABLED);
                // 增加属性
                $('.site-demo-active').attr('disabled', 'disabled');
                var formData = new FormData(document.getElementById("formDId"));
                console.log(formData)
                let res = postAjaxFile(getRootPath() + "OaRegimeAction/updateOrAdd", formData);
                if (res.code === 1) {
                    layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                    setTimeout(function () {
                        formClose();
                    }, 3000);
                    setTimeout(function () {
                        parent.layui.table.reload('test');//刷新父页面
                        //window.parent.location.reload();//刷新父页面
                        const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                    }, 3000);
                    //window.location.reload();//刷新页面
                } else if (res.code === 500) {
                    error('500');
                } else {
                    layer.msg("新增失败");
                }
                return false;
            });
            $("#demo3").on("click", function () {
                //询问框
                layer.confirm('是否关闭该页面？', {
                    btn: ['是的', '取消'] //按钮
                }, function () {
                    formClose();
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                });
            });
            loadType();
            form.render();
            var id = getQueryString("pkRegimeId");
            if (id) {
                findById();
                var str = $("#fileName").val();
                if (str.length > 1) {
                    $("#tsone").html(str.substring(0, 13) + "。。。。。 ." + str.match(/\.([^\.]+)$/)[1]);
                    $("#tsone").prop("href", str);
                }
            }
        });

        function findById() {
            var id = getQueryString("pkRegimeId");
            var url = getRootPath() + "OaRegimeAction/findById/" + id;
            var data = getAjaxById(url);
            $("#pkRegimeId").val(data.data.pkRegimeId);
            $("#regimeTypeId").val(data.data.regimeTypeId);
            $("#departmentId").val(data.data.departmentId);
            $("#regimeName").val(data.data.regimeName);
            $("#effectTime").val(data.data.effectTime);
            $("#expiresTime").val(data.data.expiresTime);
            $("#regimeAbstract").val(data.data.regimeAbstract);
            $("#regimeCode").val(data.data.regimeCode);
            $("#fileName").val(data.data.regimeFile);

            form.render();
        }

        /*哪个部门*/
        function loadType(){
            let url=getRootPath()+"OaDepartmentAction/departmentfindByAll";
            let data=getAjax(url,null);
            console.log(data.data);
            $.each(data.data,function(index,item){
                $("#departmentId").append("<option value='"+item.pkDepartmentId+"'>"+item.departmentName+"</option>");
            });
        }

    });
</script>
</body>
</html>